layui.use(['form','layer','upload','layedit'],function(){
	   var form = layui.form
       layer = parent.layer === undefined ? layui.layer : top.layer,
       $ = layui.jquery,
       laydate = layui.laydate,
       laytpl = layui.laytpl,
       upload = layui.upload;
	   var layedit = layui.layedit;
  
	   layedit.set({
           //暴露layupload参数设置接口 --详细查看layupload参数说明
           uploadImage: {
               url: Feng.ctxPath + "/modular/banner/upload",
               accept: 'image',
               acceptMime: 'image/*',
               exts: 'jpg|png|gif|bmp|jpeg',
               size: 1024 * 10,
               data: {
                   name: "测试参数",
                   age: 99
               }
               , done: function (data) {
                   console.log(data);
               }
           },
           uploadVideo: {
               url: Feng.ctxPath + "/modular/banner/upload",
               accept: 'video',
               acceptMime: 'video/*',
               exts: 'mp4|flv|avi|rm|rmvb',
               size: 1024 * 10 * 2,
               done: function (data) {
                   console.log(data);
               }
           }
           , uploadFiles: {
               url: Feng.ctxPath + "/modular/banner/upload",
               accept: 'file',
               acceptMime: 'file/*',
               size: '20480',
               autoInsert: true, //自动插入编辑器设置
               done: function (data) {
                   console.log(data);
               }
           }
           //测试参数
           , backDelImg: true
           //开发者模式 --默认为false
           , devmode: true
           //是否自动同步到textarea
           , autoSync: true
           //内容改变监听事件
           , onchange: function (content) {
               console.log(content);
           }
           //插入代码设置 --hide:false 等同于不配置codeConfig
           , codeConfig: {
               hide: true,  //是否隐藏编码语言选择框
               default: 'javascript', //hide为true时的默认语言格式
               encode: true //是否转义
               , class: 'layui-code' //默认样式
           }
           , devmode: true
           , videoAttr: ' preload="none" '
           , tool: [
               'html', 'undo', 'redo', 'code', 'strong', 'italic', 'underline', 'del', 'addhr', '|', 'removeformat', 'fontFomatt', 'fontfamily', 'fontSize', 'fontBackColor', 'colorpicker', 'face'
               , '|', 'left', 'center', 'right', '|', 'link', 'unlink', 'images', 'image_alt', 'video', 'attachment', 'anchors'
               , '|'
               , 'table', 'customlink'
               , 'fullScreen', 'preview'
           ]
           , height: '500px'
       });
       var ieditor = layedit.build('layeditDemo');

	    //普通图片上传
	    var uploadInst = upload.render({
	    	elem: '#test1'
	            ,url: 'https://www.idjglobalapp.com/app-servce/v1/api/upload' //改成您自己的上传接口
	            ,method: 'POST'
	            ,done: function(res){
	                //上传完毕
	                if(res.code == 1){
	                	layer.msg('上传成功') 
	                	$('#demo1').attr('src', res.data);
	                }
	             }
	    });
	    
	    form.on('select(status)', function(data){
	    	if(data.value == '1'){
	    		$("#diva").show();
	    		$("#divb").hide();
	    	}else if(data.value == '2'){
	    		$("#divb").show();
	    		$("#diva").hide();
	    	}else{
	    		$("#diva").show();
	    		$("#divb").hide();
	    	}
	    });
	    
	    // 表单提交事件
	    form.on('submit(btnSubmit)', function (data) {
	    	var contents = layedit.getContent(ieditor);
	    	var bannerType = $("#status").find("option:selected").val();
	    	var pic =$("#demo1").attr("src");
	    	var bannerLink = $("#bannerLink").val();
	    	var data = {
	    			contents: contents,
	    			bannerType: bannerType,
	    			bannerPic: pic,
	    			bannerLink:bannerLink,
	    			bannerStatus:1
	    	}
	    	// 异步请求
	    	$.ajax({
	    		url: Feng.ctxPath + "/modular/banner/insert",
	    		data: JSON.stringify(data),
	    		type: "post",
	    		contentType: "application/json;charset=UTF-8",
	    		async: false,//关闭异步请求
	    		dataType: "json",
	    		success: function (data) {
	    			if (data.code == 200) {
	    				Feng.success("操作成功！");
	    				//传给上个页面，刷新table用
	    				 window.parent.location.reload();
	    				 var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
	    			     parent.layer.close(index);
	    			} else {
	    				Feng.success("操作失败！" + data.message);
	    			}
	    		}
	    	,
	    	error: function (data) {
	    		Feng.error("添加失败！" + data.message)
	    	}
	    	});
	    });
	    
});